package sg.edu.nus.iss.vmcs.util;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.*;
import java.awt.event.*;

import sg.edu.nus.iss.vmcs.ui.VMCSAbstractLabel;
import sg.edu.nus.iss.vmcs.ui.VMCSAbstractPanel;
import sg.edu.nus.iss.vmcs.ui.VMCSAbstractTextField;
import sg.edu.nus.iss.vmcs.ui.VMCSEventHandler;
import sg.edu.nus.iss.vmcs.ui.VMCSUIFactory;

/**
 * This class represents the boundary object that displays an updatable text fields
 * next to the fixed label. This class creates the instance of customizable user control
 * which is formed from the components of Java AWT TextField and Label. This class inherits
 * from the Java AWT Panel class. 
 *
 * @version 1.0  09/12/2011
 * @author Team 7S - SE19
 */

public class LabelledDisplay {

/**
 * Default Value 
 */	
	public final static int DEFAULT = 5;
/**
 * Flow Layout Value
 */	
	public final static int FLOW = 1;
/**
 * Grid Layout Value
 */	
	public final static int GRID = 2;

	private VMCSAbstractPanel panel;
	private VMCSAbstractTextField value;
	private VMCSAbstractLabel lb;

/**
 * This constructor creates a new instance of the LabelledDisplay.
 * @param label Label for the component
 * @param length Length for the TextField
 * @param lt Type of the Layout for the UserControl
 */	
	public LabelledDisplay(String label, int length, int lt) {

		panel = VMCSUIFactory.getInstance().createPanel();
		if (lt == FLOW)
			panel.setLayout(new FlowLayout(FlowLayout.CENTER));
		else if (lt == GRID)
			panel.setLayout(new GridLayout(1, 2));

		lb = VMCSUIFactory.getInstance().createLabel();
		lb.setText(label);
		System.out.println("LabelledDisplay:" + label);
		lb.setAlignment(Label.RIGHT);
		VMCSAbstractPanel tp = VMCSUIFactory.getInstance().createPanel();
		tp.setLayout(new FlowLayout());
		value = VMCSUIFactory.getInstance().createTextField();
		value.setColumns(length);

		tp.add(value);
		panel.add(lb);
		panel.add(tp);
	}
	
	public VMCSAbstractPanel getLabelledDisplay() {
		return panel;
	}

/**
 * This operation attaches a listener to the LabelledDisplay component.
 * @param listener ActionListener.
 */	
	public void addListener(VMCSEventHandler listener) {
		value.addEventHandler(listener);
	}

/**
 * This operation sets whether the TextField in the Customizable UI object is editable. 
 * @param v boolean state to set TextField editable (TRUE / FALSE)
 */	
	public void setEditable(boolean v) {
		value.setEditable(v);
		if (v == false) {
			value.setBackground(192, 192, 192);
		}
	}

/**
 * This operation sets the text to be displayed.
 * @param vl String value to set into the TextField
 */	
	public void setValue(String vl) {
		value.setText(vl);
	}

/**
 * This operation sets the integer value to be displayed.
 * @param vl Integer value to be set
 */	
	public void setValue(int vl) {
		setValue(String.valueOf(vl));
	}

/**
 * This operation activates the LabelledDisplay if the Parameter is TRUE. 
 * Otherwise, the LabelledDisplay is deactivated.
 * @param st boolean state to set the status TRUE or FALSE
 */	
	public void setActive(boolean st) {
		System.out.println("LabelledDisplay.setActive");
		value.setEnabled(st);
		lb.setEnabled(st);
	}

/**
 * This operation sets the Background color of the TextField. 
 * @param c Color
 */	
	public void setTextBackground(int r, int g, int b) {
		value.setBackground(r,g,b);
	}

/**
 * This operation sets the Foreground color of the TextField.
 * @param c Color
 */	
	public void setTextForeground(int r, int b, int g) {
		value.setForeground(r,g,b);
	}

}